
$(function(){
	$("#clear").click(function(){
		localStorage.clear();
	})
	var winPrize=[],prize,firstNumber=[0];
	var arrs=[],n,index;
	var timer1,timer2,timer3;
	if(!localStorage.pond){
		for(var i=100;i<=200;i++){
			arrs.push(i);
			if(i==200){
				for(var i=201;i<=400;i++){
					arrs.push(i);
					if(i == 400){
						for(var i=400;i<=600;i++){
							arrs.push(i);
						}
					}
				}
			}
		}
		localStorage.pond = JSON.stringify(arrs);
	}
	prize=JSON.parse(localStorage.pond);
	n=prize.length-1;
	if(!localStorage.winPrize){
		localStorage.winPrize=JSON.stringify(winPrize);
	}
	if(!localStorage.grand){
		localStorage.grand=false;
	}
	if(!localStorage.first){
		localStorage.first="false";
	}
	if(!localStorage.firstNumber){
		localStorage.firstNumber=JSON.stringify(firstNumber);
	}
	if(!localStorage.second){
		localStorage.second=false;
	}
	if(!localStorage.third){
		localStorage.third=false;
	}
	if(!localStorage.normal){
		localStorage.normal=false;
	}
	if(!localStorage.add){
		localStorage.add=false;
	}

	$("#normal").click(function(){
		$(this).addClass("start");
		$(this).siblings().removeClass("start");
			$("div.prize-number").html("");
			$("#grand").css("background-image","url(img/grandprizeOff.png)");
			$("#normal").css("background-image","url(img/normalprizeOn.png)");
			$("#third").css("background-image","url(img/thirdprizeOff.png)");
			$("#second").css("background-image","url(img/secondprizeOff.png)");
			$("#first").css("background-image","url(img/firstprizeOff.png)");
			$("#add").css("background-image","url(img/additionalOff.png)");
		
	});
	
	$("#third").click(function(){
		$(this).addClass("start");
		$(this).siblings().removeClass("start");
			$("div.prize-number").html("");
			$("#grand").css("background-image","url(img/grandprizeOff.png)");
			$("#normal").css("background-image","url(img/normalprizeOff.png)");
			$("#third").css("background-image","url(img/thirdprizeOn.png)");
			$("#second").css("background-image","url(img/secondprizeOff.png)");
			$("#first").css("background-image","url(img/firstprizeOff.png)");
			$("#add").css("background-image","url(img/additionalOff.png)");
	});
	$("#second").click(function(){
		$(this).addClass("start");
		$(this).siblings().removeClass("start");
			$("div.prize-number").html("");
			$("#grand").css("background-image","url(img/grandprizeOff.png)");
			$("#normal").css("background-image","url(img/normalprizeOff.png)");
			$("#third").css("background-image","url(img/thirdprizeOff.png)");
			$("#second").css("background-image","url(img/secondprizeOn.png)");
			$("#first").css("background-image","url(img/firstprizeOff.png)");
			$("#add").css("background-image","url(img/additionalOff.png)");
	});
	$("#first").click(function(){
		$(this).addClass("start");
		$(this).siblings().removeClass("start");
			$("div.prize-number").html("");
			$("#grand").css("background-image","url(img/grandprizeOff.png)");
			$("#normal").css("background-image","url(img/normalprizeOff.png)");
			$("#third").css("background-image","url(img/thirdprizeOff.png)");
			$("#second").css("background-image","url(img/secondprizeOff.png)");
			$("#first").css("background-image","url(img/firstprizeOn.png)");
			$("#add").css("background-image","url(img/additionalOff.png)");
	});
	$("#grand").click(function(){
			$(this).addClass("start");
			$(this).siblings().removeClass("start");
			$("div.prize-number").html("");
			$("#grand").css("background-image","url(img/grandprizeOn.png)");
			$("#normal").css("background-image","url(img/normalprizeOff.png)");
			$("#third").css("background-image","url(img/thirdprizeOff.png)");
			$("#second").css("background-image","url(img/secondprizeOff.png)");
			$("#first").css("background-image","url(img/firstprizeOff.png)");
			$("#add").css("background-image","url(img/additionalOff.png)");
	});
	$("#add").click(function(){
		$(this).addClass("start");
		$(this).siblings().removeClass("start");
		$("div.prize-number").html("");
		$("#grand").css("background-image","url(img/grandprizeOff.png)");
		$("#normal").css("background-image","url(img/normalprizeOff.png)");
		$("#third").css("background-image","url(img/thirdprizeOff.png)");
		$("#second").css("background-image","url(img/secondprizeOff.png)");
		$("#first").css("background-image","url(img/firstprizeOff.png)");
		$("#add").css("background-image","url(img/additionalOn.png)");
});
	$("#start").click(function(){
		$(this).attr("disabled",true);
		var me=$(this);
		$("div.prize button").attr("disabled",true);
		if($("#grand").hasClass("start")){
			$("#again").attr("disabled",false);
			if(localStorage.grand != "true"){
				grand("grand");	
				localStorage.grand = "begin"
				$("#grand").attr("disabled",true);
				$("#grand").removeClass("past");
			}else if(localStorage.grand == "true"){
				alert("该奖项已经抽过了");
				$("button.past").attr("disabled",false);
				$("#start").attr("disabled",false);
			}
		}
		if($("#first").hasClass("start")){
			$("#again").attr("disabled",false);
			console.log(parseInt(localStorage.first))
			firstNumber=JSON.parse(localStorage.firstNumber);
			if(firstNumber[0] <3 ){
				if(localStorage.first == 'begin' || localStorage.first == 'false'){
					grand("first");	
					//$("#first").attr("disabled",true);
					localStorage.first = "begin"
				}
			}else{
				alert("该奖项已经抽过了");
				$("button.past").attr("disabled",false);
				$("#start").attr("disabled",false);
			}
		}
		if($("#second").hasClass("start")){
			if(localStorage.second == "false"){
				console.log(localStorage.second)
				var i=0;
				start(10,"second");
				$("#second").removeClass("past");
			}else if(localStorage.second == "true"){
				alert("该奖项已经抽过了");
				$("button.past").attr("disabled",false);
				$("#start").attr("disabled",false);
			}
		}
		if($("#third").hasClass("start")){
			if(localStorage.third == "false"){
				console.log(localStorage.third)
				var i=0;
				start(25,third);
				$("#third").removeClass("past");
			}else if(localStorage.third == "true"){
				alert("该奖项已经抽过了");
				$("button.past").attr("disabled",false);
				$("#start").attr("disabled",false);
			}
			
		}
		if($("#normal").hasClass("start")){
			if(localStorage.normal == "false"){
				console.log(localStorage.normal)
				var i=0;
				start(50,normal);
				$("#normal").removeClass("past");
			}else if(localStorage.normal == "true"){
				alert("该奖项已经抽过了");
				$("button.past").attr("disabled",false);
				$("#start").attr("disabled",false);
			}	
		}
		if($("#add").hasClass("start")){
			localStorage.add='begin';
			grand("add");
		}
	})
	$("#again").click(function(){
		$(this).attr('disabled',true);
		winPrize=JSON.parse(localStorage.winPrize);
		console.log(isCon(winPrize,prize[index]));
		var timer3=setInterval(function(){
			console.log(isCon(winPrize,prize[index]));
			if(isCon(winPrize,prize[index]) ==false){
				clearInterval(timer1);
				clearInterval(timer3);
				winPrize.push(prize[index]);
				localStorage.winPrize = JSON.stringify(winPrize);
				if(localStorage.grand == "begin"){
					$(".prize-number").append("<span class='grand'>"+prize[index]+"</span>");
					localStorage.grand="true";
					$("button.past").attr("disabled",false);
					$("#start").attr("disabled",false);
				}
				if(localStorage.first == 'begin'){
					$(".prize-number").append("<p class='first'>"+prize[index]+"</span>");
					$("button").attr("disabled",true);
					firstNumber=JSON.parse(localStorage.firstNumber);
					firstNumber[0]=firstNumber[0]+1
					localStorage.firstNumber = JSON.stringify(firstNumber);
					$("#start").attr("disabled",false);
					if(firstNumber[0] == 3){
						localStorage.first="true";
						$("#first").removeClass("past");
						$("button.past").attr("disabled",false);
					}
				}
				if(localStorage.add == "begin"){
					$(".prize-number").append("<span class='grand'>"+prize[index]+"</span>");
					localStorage.grand="false";
				}
			}
		},100)
	})
	var array=[];
	function start(index,item){
		var number="",i=0;
		winPrize=JSON.parse(localStorage.winPrize)
		console.log(winPrize);
		timer2=setInterval(function(){
			number=parseInt(Math.random()*n);
			$("#thousand").text(prize[number]);
			if(i != 0 && i%30 == 0){
				console.log(isCon(winPrize,prize[number]));
				if(isCon(winPrize,prize[number]) == false){
					$(".prize-number").append("<span class='normal'>"+prize[number]+"</span>");
					winPrize.push(prize[number]);
					localStorage.winPrize = JSON.stringify(winPrize);
					index--;
				}
				if(isCon(winPrize,prize[number]) == true){
					i++;
				}
			}
			if(index == 0){
				clearInterval(timer2);
				$("#start").attr("disabled",false);
				$("button.past").attr("disabled",false);
				if(item == "second"){
					localStorage.second=true;
					return;
				}
				if(item == "third"){
					localStorage.third=true;
					return;
				}
				if(item == "normal"){
					localStorage.normal=true;
					return;
				}
			}
			i++;
		},100)
	}
	function grand(item){
		timer1=setInterval(function(){
			index=parseInt(Math.random()*n);
			$("#thousand").text(prize[index]);
		},100)
		if(item == "grand"){
			localStorage.grand="begin";
		}
	}
	//判断是否为重复的数字
	function isCon(arr,val){
		for(var i=0; i<arr.length; i++){
			if(arr[i] == val)
			return true;
			}
			return false;
	}

})
//一等奖开始结束数字全部出来